﻿using supermarket.method;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace supermarket
{
    public partial class shopmanage : Form
    {
        public SqlConnection sql = new Classgetsql().getcon();
        public shopmanage ()
        {
            InitializeComponent();
        }

        private void button1_Click ( object sender, EventArgs e )
        {
            Newstores newstores = new Newstores();
            newstores.ShowDialog();
            Thread.Sleep(5000);
            LoadData(dgv_shoplist, "shop");
        }

        private void button2_Click ( object sender, EventArgs e )
        {
            frm_shopData frm = new frm_shopData();
            frm.Show();
        }

        private void shopmanage_Load ( object sender, EventArgs e )
        {
            LoadData(dgv_shoplist, "shop");
            dgv_shoplist.CellContentClick += Dgv_shoplist_CellContentClick;
            DataGridViewButtonColumn editButtonColumn = new DataGridViewButtonColumn//编辑
            {
                Name = "colEdit",
                HeaderText = "操作",
                Text = "编辑",
                UseColumnTextForButtonValue = true
            };
            dgv_shoplist.Columns.Add(editButtonColumn);


            DataGridViewButtonColumn deleteButtonColumn = new DataGridViewButtonColumn// 删除
            {
                Name = "colDelete",
                HeaderText = "操作",
                Text = "删除",
                UseColumnTextForButtonValue = true
            };
            dgv_shoplist.Columns.Add(deleteButtonColumn);
        }

        #region 加载信息

        public void LoadData ( DataGridView dgv, string tableType )
        {
            try
            {
                string query;
                Classgetsql sqlcon = new Classgetsql();
                SqlConnection con = sqlcon.getcon();
                SqlDataAdapter adapter;
                DataTable dataTable = new DataTable();

                switch (tableType.ToLower())
                {
                   
                    case "shop":
                        query = @"SELECT TOP 1000 shop_code, shop_name, shop_location, 
                            shop_manager, shop_phoneNum, shop_Opened, 
                            shop_region 
                            FROM [super].[dbo].[shop_List]";
                        adapter = new SqlDataAdapter(query, con);
                        adapter.Fill(dataTable);
                        dgv.DataSource = dataTable;
                        // 设置店铺表列标签
                        dgv.Columns["shop_code"].HeaderText = "店铺编号";
                        dgv.Columns["shop_name"].HeaderText = "店铺名称";
                        dgv.Columns["shop_location"].HeaderText = "店铺位置";
                        dgv.Columns["shop_manager"].HeaderText = "店铺经理";
                        dgv.Columns["shop_phoneNum"].HeaderText = "联系电话";
                        dgv.Columns["shop_Opened"].HeaderText = "开业时间";
                        dgv.Columns["shop_region"].HeaderText = "区域";
                        //dgv.Columns["shop_grade"].HeaderText = "店铺等级";
                        //dgv.Columns["shop_proformance"].HeaderText = "店铺绩效";
                        break;
                 
                }
                con.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("加载数据时发生错误: " + ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        #endregion

        #region 门店信息维护
        private void Dgv_shoplist_CellContentClick ( object sender, DataGridViewCellEventArgs e )
        {
            // 检查是否点击了编辑按钮列
            if (e.ColumnIndex == dgv_shoplist.Columns["colEdit"].Index && e.RowIndex >= 0)
            {
                // 获取当前行的数据
                DataGridViewRow row = dgv_shoplist.Rows[e.RowIndex];
                string shopCode = row.Cells["shop_code"].Value.ToString();
                string shopName = row.Cells["shop_name"].Value.ToString();
                string shopLocation = row.Cells["shop_location"].Value.ToString();
                string shopManager = row.Cells["shop_manager"].Value.ToString();
                string shopPhoneNum = row.Cells["shop_phoneNum"].Value.ToString();
                string shopOpened = row.Cells["shop_Opened"].Value.ToString();
                string shopRegion = row.Cells["shop_region"].Value.ToString();

                // 打开编辑窗体，传递当前行数据
                EditShopForm editForm = new EditShopForm(shopCode, shopName, shopLocation,
                                                        shopManager, shopPhoneNum, shopOpened, shopRegion);
                if (editForm.ShowDialog() == DialogResult.OK)
                {
                    // 编辑成功后刷新数据
                    LoadData(dgv_shoplist, "shop");
                }

            }
            // 检查是否点击了删除按钮列
            else if (e.ColumnIndex == dgv_shoplist.Columns["colDelete"].Index && e.RowIndex >= 0)
            {
                // 获取当前行的店铺编码
                DataGridViewRow row = dgv_shoplist.Rows[e.RowIndex];
                string shopCode = row.Cells["shop_code"].Value.ToString();
                string shopName = row.Cells["shop_name"].Value.ToString();

                // 弹出确认对话框
                DialogResult result = MessageBox.Show($"确定要删除店铺 '{shopName}' (编码: {shopCode}) 吗？",
                                                     "确认删除", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
                if (result == DialogResult.Yes)
                {
                    try
                    {
                        using (SqlCommand cmd = new SqlCommand())
                        {
                            cmd.Connection = sql;

                            string query1 = "DELETE FROM super.dbo.shop_List WHERE shop_code = @shopCode";
                            cmd.CommandText = query1;
                            cmd.Parameters.Clear();
                            cmd.Parameters.AddWithValue("@shopCode", shopCode);
                            int rowsAffected1 = cmd.ExecuteNonQuery();

                            string query2 = "DELETE FROM super.dbo.shop_sales WHERE shop_code = @shopCode";
                            cmd.CommandText = query2;

                            int rowsAffected2 = cmd.ExecuteNonQuery();

                            if (rowsAffected1 > 0 || rowsAffected2 > 0)
                            {
                                MessageBox.Show("删除成功！", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
                                LoadData(dgv_shoplist, "shop"); // 刷新数据
                                Thread.Sleep(100);
                            }
                            else
                            {
                                MessageBox.Show("删除失败，未找到对应记录！", "错误",
                                               MessageBoxButtons.OK, MessageBoxIcon.Error);
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show($"删除数据时发生错误: {ex.Message}", "错误",
                                       MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }
        }
        #endregion
    }
}
